package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 周信息数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "周信息")
public class WeekDTO {

    @Schema(description = "第几周", example = "1")
    private int weekNumber;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "周开始日期", example = "2024-09-01")
    private LocalDate startDate;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "周结束日期", example = "2024-09-07")
    private LocalDate endDate;

    @Schema(description = "显示名称 如\"第1周 (9月1日-9月7日)\"", example = "第1周 (9月1日-9月7日)")
    private String displayName;

    /**
     * 自动生成显示名称
     */
    public String getDisplayName() {
        if (displayName == null && startDate != null && endDate != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日");
            String startStr = startDate.format(formatter);
            String endStr = endDate.format(formatter);
            return "第" + weekNumber + "周 (" + startStr + "-" + endStr + ")";
        }
        return displayName;
    }

    /**
     * 获取周总天数
     */
    public int getTotalDays() {
        if (startDate != null && endDate != null) {
            return (int) java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1;
        }
        return 0;
    }
}
